package cn.factorybox.item.service.impl;

import cn.factorybox.item.mapper.SystemLogMapper;
import cn.factorybox.item.service.SystemLogService;
import cn.factorybox.model.entity.SystemLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-04-20 11:36
 */
@Service
public class SystemLogServiceImpl implements SystemLogService {

    @Autowired
    private SystemLogMapper systemLogMapper;

    @Override
    public int addlog(SystemLog log) {
        return systemLogMapper.addLog(log);
    }

    @Override
    public Map<String, Object> selectTodayUserLogin() {
        List<Map<String,Object>> loginRecords = systemLogMapper.selectLoginRecords();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        for (Map<String,Object> map : loginRecords) {
            String time = sdf.format((Date)map.get("time"));
            map.put("time",time);
            if (map.get("portrait") == null) {
                map.put("portrait","");
            }
        }
        Map<String,Object> maps = new HashMap<>();
        maps.put("data",loginRecords);
        return maps;
    }

    @Override
    public Map<String, Object> selectOperationLogs() {
        List<Map<String,Object>> logs = systemLogMapper.selectOperationLogs();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
        for (Map<String,Object> map : logs) {
            String time = sdf.format((Date)map.get("time"));
            map.put("time",time);
        }
        Map<String,Object> maps = new HashMap<>();
        maps.put("data",logs);
        return maps;
    }
}
